package com.example.movie_demo

import android.content.Context
import android.util.Log
import androidx.work.Worker
import androidx.work.WorkerParameters
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class UploadWorker(appContext: Context, workerParams: WorkerParameters):
       Worker(appContext, workerParams) {
   override fun doWork(): Result {
       val json = JSONObject()
       json.put("date",Data().toString())
       json.put("username",inputData.getString("username"))
       json.put("event",inputData.getString("event"))
       Log.d(MainActivity.TAG, "params:$json"+ MainActivity.URL)

       return uploadLog(json,MainActivity.URL)
   }

   fun uploadLog(json:JSONObject,url:String): Result {
       TrackerRetrofitService.create(url).postLog(json).enqueue(object :Callback<String>{
           override fun onResponse(call: Call<String>, response: Response<String>) {
               Log.d(MainActivity.TAG, "onSuccess:${response.body()}")
           }

           override fun onFailure(call: Call<String>, t: Throwable) {
               Log.e(MainActivity.TAG, "onFailure: ")
           }

       })
       return Result.success()
    }
}
